package com.fastloan.jn.mgt.customermanager.daily.controller;

import com.fastloan.jn.mgt.customermanager.daily.model.JnCustomerManagerDaily;
import com.fastloan.jn.mgt.customermanager.daily.service.JnCustomerManagerDailyService;
import com.fastloan.jn.utils.other.ObjUtil;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanwe
 * createTime 2017-10-2017/10/19 14:21
 */
@RestController
@RequestMapping("/api/web/customerManagerDaily")
public class JnCustomerManagerDailyController extends BaseController<JnCustomerManagerDaily>{

    @Autowired
    private JnCustomerManagerDailyService jnCustomerManagerDailyService;

    @GetMapping
    public ResultTo getDaily(@RequestParam(value = "userId",required = false) String userId) {
        return new ResultTo().setData(jnCustomerManagerDailyService
                .getJnCustomerManagerDailyByUserId(userId == null ? "1" : userId));
    }

    @PostMapping
    public ResultTo addDaily(JnCustomerManagerDaily jnCustomerManagerDaily){
        boolean result = jnCustomerManagerDailyService.addDaily(jnCustomerManagerDaily);
        return new ResultTo(result?ResultEnum.SUCCESS:ResultEnum.OPERATION_FAILED);
    }

    /**
     * 顶级机构下所有客户经理日报
     * @return
     */
    @PostMapping("/allDalies")
    public ResultTo allDalies(String cname){
        //@Todo 分角色
        List<String> ids = jnCustomerManagerDailyService.topOrgManagerIds("1");
        Map<String,Object> map = new HashMap<>();
        map.put("list",ids);
        if(!ObjUtil.isEmpty(cname)) { map.put("cname",cname.replace("#","")); }
        return dataPage("managerDaliy",map);
    }

    /**
     * 经理日报详情
     * @param daliyId
     * @return
     */
    @GetMapping("/daliyDetail")
    public ResultTo daliyDetail(String daliyId){
        JnCustomerManagerDaily managerDaily = jnCustomerManagerDailyService.selectByPrimaryKey(daliyId);
        return new ResultTo().setData(managerDaily);
    }

    /**
     * 更新日报
     * @param managerDaily
     * @return
     */
    @PostMapping("/updateDaliy")
    public ResultTo updateDaliy(JnCustomerManagerDaily managerDaily){
        if(jnCustomerManagerDailyService.updateByPrimaryKeySelective(managerDaily) == 1){
            return new ResultTo();
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED);
    }



}
